import Vue from 'vue'
import VueRouter from 'vue-router'
import { routes } from './router'
import inject from '@/plugins/inject'

import '@/plugins/iview'
import 'iview/dist/styles/iview'
Vue.prototype.$IVIEW = { size: '' }

import filterRouter from '@/plugins/filterRouter'

Vue.use(inject)
Vue.use(VueRouter)

const RouterConfig = {
  routes: filterRouter(routes),
  mode: 'history'
}
const router = new VueRouter(RouterConfig)

export default router

router.beforeEach((to, from, next) => {
  Vue.prototype.$Loading.start()
  if (to.path !== '/login') {
    if (Vue.prototype.$cookie.get('token')) {
      next()
    } else {
      next({ path: '/login', replace: true })
    }
  } else {
    next()
  }
})

router.afterEach((to, from, next) => {
  Vue.prototype.$Loading.finish()
})
